/**
 * 
 */
package com.svds.databaseutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Krishna
 *
 */
public class MyConnection {
	
	public static Connection conn = null;
	
	public void MyConnection()
	{
	}
	
	public static Connection getConnection()
	{
		String userName = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/onlinedrivingschool";
        try {
			Class.forName ("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, userName, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        System.out.println ("Connected");
        return conn;
	}
	
	private boolean doLogin(String userName,String password)
	{
		String query = "select * from users where username='"+userName+"'";
		
		if(conn == null)
		{
			getConnection();
		}
		try
		{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			if(rs.next())
			{
				String user = rs.getString(2);
				String pwd = rs.getString(3);
				
				System.out.println(user + "   <<  >>  " + pwd);
				
				if(userName.equalsIgnoreCase(user) && password.equals(pwd))
				{
					return true;
				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return true;
		}
		
		
		return false;
	}
	
	public static void main(String a[])
	{
		MyConnection mycon = new  MyConnection();
		System.out.println(mycon.doLogin("admin", "admin"));
	}

}
